// Copyright (c) 2011, Grant China
// All rights reserved.

package org.gwtlcars.sample;

import org.gwtlcars.ui.LcarsColor;
import org.gwtlcars.ui.LcarsPanel;
import org.gwtlcars.ui.LcarsPanelBottomLeft;
import org.gwtlcars.ui.LcarsPanelButton;
import org.gwtlcars.ui.LcarsPanelButton.State;
import org.gwtlcars.ui.LcarsPanelTopLeft;
import org.gwtlcars.ui.LcarsRadioButton;
import org.gwtlcars.ui.LcarsRadioButtonGroup;
import org.gwtlcars.ui.LcarsRoundButton;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GwtLcarsSample implements EntryPoint {

    private static final LcarsPanel topPanel = new LcarsPanelBottomLeft();
    private static final LcarsPanel mainPanel = new LcarsPanelTopLeft();
    private static final DeckPanel main_deck = new DeckPanel();

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        LcarsColor.INSTANCE.lcarsColor().ensureInjected();
        Window.enableScrolling(false);

        // FIXME: there must be a better way to set the initial size of the main
        //        panel than this.
        mainPanel.addAttachHandler(new AttachEvent.Handler() {
            @Override
            public void onAttachOrDetach(AttachEvent event) {
                String height = RootPanel.get("top_panel").getElement()
                        .getAttribute("style");
                height = height.split(";")[0].split(":")[1].split("p")[0];
                mainPanel.setHeight(Window.getClientHeight()
                        - Integer.parseInt(height) - 10 + "px");
            }
        });

        RootPanel.get("top_panel").add(topPanel);
        RootPanel.get("bottom_panel").add(mainPanel);

        topPanel.setWidth("100%");
        mainPanel.setWidth("100%");

        topPanel.addVerticalButton("Button A");
        topPanel.addVerticalButton("Button B");
        topPanel.addVerticalButton("Button C");
        topPanel.addVerticalButton("Button D");
        topPanel.addHorizontalButton("Button E");
        LcarsPanelButton barButton = topPanel.addHorizontalButton("Button F");
        barButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                Window.alert("Panel Button F");
            }
        });

        HTML myLabel = new HTML(
                "<p style='color:#f90;font-family:arial;font-style:italic;font-size:3em'>GWT LCARS Sample App 0.0.3</p>");
        topPanel.add(myLabel);

        LcarsPanelButton roundBtnBtn = mainPanel.addHorizontalRadioButton(
                "Round Btns", "group");
        roundBtnBtn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                main_deck.showWidget(0);
            }
        });
        LcarsPanelButton radioBtnBtn = mainPanel.addHorizontalRadioButton(
                "Radio Btns", "group");
        radioBtnBtn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                main_deck.showWidget(1);
            }
        });

        mainPanel.addVerticalButton("Button G");
        mainPanel.addVerticalButton("Button H");
        mainPanel.addVerticalPushButton("Button I");
        LcarsPanelButton cButton = mainPanel.addVerticalPushButton("STATUS");
        cButton.setUpColor(LcarsColor.LIGHT_RED);
        cButton.setDownColor(LcarsColor.LIGHT_GREEN);

        main_deck.add(initRoundBtnTab());
        main_deck.add(initRadioBtnTab());

        mainPanel.add(main_deck);
        roundBtnBtn.setState(State.DOWN);
        main_deck.showWidget(0);

        Window.addResizeHandler(new ResizeHandler() {
            @Override
            public void onResize(ResizeEvent event) {
                // set height of mainPanel to remainder of screen
                int height = event.getHeight();
                mainPanel.setHeight(height - topPanel.getOffsetHeight() + "px");
            }
        });
    }

    private Widget initRoundBtnTab() {
        FlowPanel vp = new FlowPanel();

        String button_idxs[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i",
                "j", "k", "l", "m", "n" };
        for (String b : button_idxs) {
            LcarsRoundButton round_button = new LcarsRoundButton(
                    "Round Button " + b);
            DOM.setStyleAttribute(round_button.getElement(), "margin", "1em");
            vp.add(round_button);
        }

        return vp;
    }

    private Widget initRadioBtnTab() {
        HorizontalPanel hp = new HorizontalPanel();

        LcarsRadioButtonGroup rbg_a = new LcarsRadioButtonGroup("rb group a");
        rbg_a.add(new LcarsRadioButton("Radio Button a"));
        rbg_a.add(new LcarsRadioButton("Radio Button b"));
        rbg_a.add(new LcarsRadioButton("Radio Button c"));
        LcarsRadioButton rb_d = new LcarsRadioButton("Radio Button d");
        rb_d.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                Window.alert("Radio Button d");
            }
        });
        rbg_a.add(rb_d);
        DOM.setStyleAttribute(rbg_a.getElement(), "margin", "2em");
        hp.add(rbg_a);

        LcarsRadioButtonGroup rbg_b = new LcarsRadioButtonGroup("rb group b");
        rbg_b.add(new LcarsRadioButton("Radio Button e"));
        rbg_b.add(new LcarsRadioButton("Radio Button f"));
        rbg_b.add(new LcarsRadioButton("Radio Button g"));
        rbg_b.add(new LcarsRadioButton("Radio Button h"));
        DOM.setStyleAttribute(rbg_b.getElement(), "margin", "2em");
        hp.add(rbg_b);

        return hp;
    }
}
